// 1. 获得客户端的ip
function getClientIp(req) {
  return (
    req.headers['x-forwarded-for'] ||
    req.connection.remoteAddress ||
    req.socket.remoteAddress ||
    req.connection.socket.remoteAddress
  )
}

// 2. 获得url中的查询参数
function getParams(url) {
  // url = '/users/?pagenum=1&pagesize=10&query=ts'
  let index = url.indexOf('?')
  if (index === -1) {
    return {}
  }
  let str = url.substring(index + 1) // pagenum=1&pagesize=10&query=ts
  let arr = str.split('&') // ['pagenum=1','pagesize=10','query=ts']
  let obj = {}
  for (let item of arr) {
    let list = item.split('=') // ['pagenum', '1']
    obj[list[0]] = list[1]
  }
  return obj
}

// 导出 CommonJS
module.exports = { getClientIp, getParams }
